<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 会吃草的鱼<783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
use Weixin\Api\WxuserApi;
use Shop\Api\WithdrawApi;
class AccountController extends HomeController {
    /*
     * 主页余额
     * */
    public function index(){
        $userinfomap['id'] = $this->userinfo['id'];
        $userinfores =  apiCall(WxuserApi::GET_INFO, array($userinfomap));
        if($userinfores['status']){
            $this->assign('money',$userinfores['info']['money']);
        }
        $this->display();
    }
    /*
     * 绑定支付宝
     * */
    public function bind_alipay(){
        $this->display();
    }
    /*
     * 申请提现
     * */
    public function apply_money(){
        if(IS_POST){
            $userinfomap['id'] = $this->userinfo['id'];
            $userinfores =  apiCall(WxuserApi::GET_INFO, array($userinfomap));
            if($userinfores['status']){
                $residue = bcadd($userinfores['info']['money'],2) - (bcadd(I('money'),2));
                if($residue >= 0){
                    if(I('money')<=0) $this->error('您输入的金额不正确');
                    $withdraw['money'] = I('money');
                }else{
                    $this->error('对不起您的账户并没有这么多余额',U('Account/index'));
                }
            }
            $withdraw['uid'] = $this->userinfo['id'];
            $withdraw['create_time'] = time();
            $withdraw['update_time'] = time();
            $withdraw['account_name'] = I('account_name');
            $withdraw['cash_account'] = I('account');
            $withdraw['bank_branch'] = '支付宝';
            $withdraw['status'] = 0;
            if(empty(I('money'))||empty(I('account_name'))||empty(I('account'))) $this->error('信息不全');
            $withdrawres = apiCall(WithdrawApi::ADD,array($withdraw));
            if($withdrawres['status']){
                //提现申请申请成功后将Wuser中的钱减去
                $moneymap['money'] = $residue;
                $userinfores =  apiCall(WxuserApi::SAVE, array($userinfomap,$moneymap));
                if($userinfores['status']){
                    $this->success('申请成功',U('Account/index'));
                }
            }else{
                $this->error('申请失败',U('Account/index'));
            }

        }else{
            $userinfomap['id'] = $this->userinfo['id'];
            $userinfores =  apiCall(WxuserApi::GET_INFO, array($userinfomap));
            if($userinfores['status']){
                $this->assign('money',$userinfores['info']['money']);
            }
            $this->display();
        }
        
    }
    /*
     * 历史提现
     */
    public function his_apmoney(){
        $wxusermap['id'] = $this->userinfo['id'];
        $userinfores =  apiCall(WxuserApi::GET_INFO, array($wxusermap));
        if($userinfores['status']){
            $this->assign('money',$userinfores['info']['money']);
        }
        $withdrawmap['uid'] = $wxusermap['id'];
        $withdrawres = apiCall(WithdrawApi::QUERY_NO_PAGING,array($withdrawmap));
        if($withdrawres['status']){
            $this->assign('his_apply',$withdrawres['info']);
        }
        $this->display();
    }
    /*
     * 提现追踪
     * */
    public function apply_trace(){
        $wxusermap['id'] = $this->userinfo['id'];
        $userinfores =  apiCall(WxuserApi::GET_INFO, array($wxusermap));
        if($userinfores['status']){
            $withdrawmap['uid'] = $wxusermap['id'];
            $withdrawmap['id'] = I('id');
            $withdrawres = apiCall(WithdrawApi::GET_INFO,array($withdrawmap));
            if($withdrawres['status']){
                $this->assign('tracedraw',$withdrawres['info']);
                $this->assign('money',$userinfores['info']['money']);
                $this->display();
            }else{
                $this->error('不好意思 出错了');
            }
        }else{
            $this->error('不好意思 出错了');
        }
    }
}